package com.example.customerservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.commonutils.JwtUtils;
import com.example.commonutils.R;
import com.example.commonutils.handler.NoqueueException;
import com.example.customerservice.client.MessageClient;
import com.example.customerservice.entity.OrderCustomer;
import com.example.customerservice.entity.vo.CustomerLoginVo;
import com.example.customerservice.entity.vo.CustomerVo;
import com.example.customerservice.mapper.OrderCustomerMapper;
import com.example.customerservice.service.OrderCustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-06-29
 */
@Service
public class OrderCustomerServiceImpl extends ServiceImpl<OrderCustomerMapper, OrderCustomer> implements OrderCustomerService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private MessageClient messageClient;
    @Override
    public String login(OrderCustomer orderCustomer) {
        String phone = orderCustomer.getPhone();
        String password = orderCustomer.getPassword();
        //手机和密码非空的判断
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)){
            throw new NoqueueException(20001,"登录失败");
        }
        LambdaQueryWrapper<OrderCustomer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderCustomer::getPhone,phone);
        OrderCustomer phoneMember = baseMapper.selectOne(queryWrapper);  // 根据手机号查询
        //判断查询对象是否为空
        if (phoneMember == null){
            throw new NoqueueException(20001,"手机号不存在");
        }
        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码先加密，再和数据库中的密码比较
        //加密方式MD5
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(phoneMember.getPassword())){
            throw new NoqueueException(20001,"登录失败");
        }
        //登录成功
        //根据查出来的用户名和id生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(phoneMember.getId(), phoneMember.getUsername());
        return jwtToken;
    }

    @Override
    public String login2(CustomerLoginVo customerLoginVo) {
        String loginType = customerLoginVo.getLoginType();
        if (loginType.equals("password")){
            String username = customerLoginVo.getUsername();
            String password = customerLoginVo.getPassword();
            if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
                throw new NoqueueException(20001,"用户名和密码不能为空");
            }
            LambdaQueryWrapper<OrderCustomer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OrderCustomer::getUsername,username);
            OrderCustomer orderCustomer = baseMapper.selectOne(queryWrapper);// 根据用户名查询
            //判断查询对象是否为空
            if (orderCustomer == null){
                throw new NoqueueException(20001,"用户不存在");
            }
            password = DigestUtils.md5DigestAsHex(password.getBytes());
            if (!password.equals(orderCustomer.getPassword())){
                throw new NoqueueException(20001,"登录失败");
            }
            String jwtToken = JwtUtils.getJwtToken(orderCustomer.getId(), orderCustomer.getUsername());
            return jwtToken;
        }else {
            String phone = customerLoginVo.getPhone();
            String validCode = customerLoginVo.getValidCode();
            if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(validCode)){
                throw new NoqueueException(20001,"手机号和验证码不能为空");
            }
            LambdaQueryWrapper<OrderCustomer> queryWrapper = new LambdaQueryWrapper<>();
            //通过手机号来查询用户信息
            queryWrapper.eq(OrderCustomer::getPhone,phone);
            OrderCustomer orderCustomer = baseMapper.selectOne(queryWrapper);
            if (orderCustomer == null){
                throw new NoqueueException(20001,"该手机号不存在，请先注册");
            }
            String s = redisTemplate.opsForValue().get(phone);
            if (validCode.equals(s)){
                String jwtToken = JwtUtils.getJwtToken(orderCustomer.getId(),orderCustomer.getUsername());
                return jwtToken;
            }else {
                throw new NoqueueException(20001,"验证码错误");
            }
        }
    }
}
